//
//  ZZCLookPayPasswordController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCLookPayPasswordController.h"
#import "ZZCLookPayPasswordView.h"
#import "ZZCSetPayPwordTweController.h"

@interface ZZCLookPayPasswordController ()
//验证码
@property (weak, nonatomic) IBOutlet UITextField *testTextField;
@property(nonatomic,strong) GRBkeyTextField * safeTextField;
@property(nonatomic,strong) GRBkeyTextField * safeInputTextField;
@property(nonatomic,strong) GRBsafeKeyBoard * board;
@property(nonatomic,strong) ZZCLookPayPasswordView *lookView;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@property (copy, nonatomic) NSString *code;
@property (copy, nonatomic) NSString *pwd;
@property (assign, nonatomic) bool isRight;


@end

@implementation ZZCLookPayPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找回支付密码";
//    _isRight = NO;
}

//获取验证码
- (IBAction)testBtnClick {
    ZZCLog(@"获取验证码");

    NSString *phone = [[NSUserDefaults standardUserDefaults] objectForKey:@"userphone"];
    NSDictionary *dict = @{@"phone":phone,@"type":@(2)};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:sign];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"phone":phone,@"type":@(2)};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_SENDCODE parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            self.code = responseObject[@"data"][@"code"];
            if ([responseObject[@"code"]isEqual:@(200)]) {
                [self codeTimerAction];
            }
        }
    }];

}

- (IBAction)certainClick {
    
    NSString *code = [NSString stringWithFormat:@"%@",self.code];
    if ([self.testTextField.text isEqualToString:code]) {
        ZZCSetPayPwordTweController *vc = [ZZCSetPayPwordTweController new];
        //        vc.titlelbael.text = @"重新设置你的支付密码";
        vc.code = self.code;
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        ZZCLog(@"验证码错误");
        [SVProgressHUD showInfoWithStatus:@"验证码错误"];
    }

//    self.board = [GRBsafeKeyBoard GRB_showSafeInputKeyBoard];
//    __weak typeof(self)weakSelf = self;
//    self.board.GRBsafeKeyFinish=^(NSString * passWord){
//        if (passWord.length==6) {
//            
//            [weakSelf.board GRB_endKeyBoard];
//            ZZCLog(@"你输入的密码为---%@",passWord);
//            _pwd = passWord;
//            //在这里判断密码是否正确
//            if (YES) {
//                ZZCLog(@"支付密码设置成功");
////                [weakSelf.view addSubview:weakSelf.lookView];
////                [weakSelf.lookView.confirmBtn addTarget:weakSelf action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
////                [weakSelf.lookView.cancelBtn addTarget:weakSelf action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
//            
//            }else {
//                ZZCLog(@"支付密码设置失败");
//            }
//        }
//        
//    };
//    self.board.GRBsafeKeyClose=^{
//        
//        [weakSelf.board GRB_endKeyBoard];
//    };
//    
//    self.board.GRBsafeKeyForgetPassWord=^{
//        ZZCLog(@"点击忘记密码了");
//        
//    };

}

//确定
- (void)confirmBtnClick {
    [self.lookView removeFromSuperview];
}
//关闭
- (void)cancelBtnClick {
    [self.lookView removeFromSuperview];
}

- (ZZCLookPayPasswordView *)lookView {
    if (_lookView == nil) {
        _lookView = [ZZCLookPayPasswordView lookPayPasswordView];
        _lookView.frame = CGRectMake(0, 0, ZZCScreenW, ZZCScreenH);
        _lookView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    }
    return _lookView;
}
- (void)codeTimerAction {
    __block int time = 60;
    __block UIButton *verifybutton = _codeBtn;
    verifybutton.enabled = NO;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(time<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //                verifybutton.backgroundColor = [UIColor whiteColor];
                [verifybutton setTitle:@"获取验证码" forState:UIControlStateNormal];
                verifybutton.enabled = YES;
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //                verifybutton.backgroundColor = [UIColor whiteColor];
                NSString *strTime = [NSString stringWithFormat:@"已发送(%d)",time];
                [verifybutton setTitle:strTime forState:UIControlStateNormal];
                verifybutton.titleLabel.textColor = [UIColor whiteColor];
            });
            time--;
        }
    });
    dispatch_resume(_timer);
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
